package bio.simplex;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PlainEchoServer {

    public static void main(String[] args) throws IOException {

        ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        try (ServerSocket socket = new ServerSocket(8888)) {
            while (true) {
                Socket client = socket.accept();
                System.out.println("accepted:" + client.getInetAddress());
                service.submit(() -> {
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()))) {
                        String line = null;
                        while (true) {
                            if ((line = reader.readLine()) != null) {
                                System.out.println(line);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }
}
